You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435
  1. const Product = require('../../../models/product');
  2. import dbConnect from '../../../utils/helpers/dbHelpers';
  3. async function handler(req, res) {
  4. const { method } = req;
  5. await dbConnect();
  6. switch (method) {
  7. case 'GET': {
  8. try {
  9. const productId = req.query.productId;
  10. const product = await Product.findOne({ customID: productId });
  11. if (!product) {
  12. throw new Error('The product with this id does not exist!');
  13. }
  14. res.status(200).json({
  15. message: 'The product you requested was fetched successfully.',
  16. product,
  17. });
  18. } catch (error) {
  19. res.status(400).json({ message: error.message });
  20. }
  21. break;
  22. }
  23. default:
  24. res.status(405).json({ message: 'Method not allowed' });
  25. break;
  26. }
  27. }
  28. export default handler;